<?php

namespace app\admin\controller;
//验证类
use app\admin\controller\Auth;
use think\View;
use think\Db;

class Index extends Auth
{
    //需验证方法集
    protected $is_check_login = ['*'];

    //后台页面
    public function page($Indexpage)
    {
        //判断退出登录
        if ($Indexpage == "logout") {
            session(null);
            $this->success('正在跳转登录页面', '/' . config('adminPath') . '/login', 1);
        }
        // 判断后台首页
        if ($Indexpage == 'home') {
            // 用户数
            $data['count']['user'] = db('v1_user')->count();
            // 卡密数
            $data['count']['kami'] = db('v1_kami')->count();
            // 留言数
            $data['count']['say'] = db('v1_say')->count();
            $this->assign([
                "data" => $data
            ]);
        }

        //视图文件路径
        $path = "/" . $Indexpage . "/" . $Indexpage;
        return view($path);
    }

    //页面初始化数据
    public function pagedata($Indexpage)
    {
        //接口名单
        $form = ['webset', "seoset", "apiset", 'appset', 'tipsset', 'collectset', 'bannerset', 'sayset', 'emailset', 'socket'];
        $from = ['navset', 'player'];
        switch ($Indexpage) {
            case 'index': //首页
                return $this->getNav($Indexpage);
                break;
            case 'platelist': //专题列表
                return $this->getPlateList();
                break;
            case 'bannerlist': //轮播列表
                return $this->getBannerList();
                break;
            case 'tort': //侵权
                return $this->getTort();
                break;
            case 'adlist': //广告列表
                return $this->getAdList();
                break;
            case 'mill': //商城列表
                return $this->getMill();
                break;
            case 'saylist': //留言列表
                return $this->getSayList();
                break;
            case 'sayinfo': //留言详情
                return $this->getStyInfo();
                break;
            case 'kamilist': //卡密列表
                return $this->getKamiList();
                break;
            case 'userlist': //会员列表
                return $this->getUserList();
                break;
            default:
                //判断是否在名单内
                if (in_array($Indexpage, $form)) {
                    //获取信息返回
                    return getConfig($Indexpage);
                }

                if (in_array($Indexpage, $from)) {
                    $data = getConfig($Indexpage);
                    $info = [
                        "code" => 0,
                        "msg" => "加载成功",
                        "count" => count($data),
                        "data" => getConfig($Indexpage)
                    ];
                    return ($info);
                }
                return;
                break;
        }
    }

    // 用户列表
    public function getUserList()
    {
        //搜索
        $searchParams = json_decode(input('searchParams'), true);
        $sqlfield = 'v1_user.user_id as id,user_name as name,user_email as email,user_time as time,user_state as "state",DATE_FORMAT(user_login_time,"%Y-%m-%d %H:%s") as oldtime,user_nick as nick,user_qq as qq,user_lv as "lv",user_grooms as grooms,user_status as status,user_points as points';
        // 分页
        $limit = $this->getPageing();
        $join = 'v1_user.user_id=v1_user_info.user_id';
        // 判断是否是搜索
        if (isset($searchParams['username'])) {
            $where['user_name'] = array('like', '%' . trim($searchParams['username']) . '%');
            $data['data'] = db('v1_user')->where($where)->join('v1_user_info', $join)->field($sqlfield)->limit($limit)->order('v1_user.user_id desc')->select();
        } else {
            // 查询信息
            $data['data'] = db('v1_user')->field($sqlfield)->join('v1_user_info', $join)->limit($limit)->order('v1_user.user_id desc')->select();
        }
        // 获取表统计
        $data['count'] = db('v1_user')->count();
        $data['code'] = 0;
        $data['msg'] = "搜索成功";
        return $data;
    }

    // 卡密列表
    public function getKamiList()
    {
        //搜索
        $searchParams = json_decode(input('searchParams'), true);
        $sqlfield = 'kami_id as id,kami_type as type,kami_content as content,kami_points as points,kami_time,DATE_FORMAT(km_create_time, "%Y-%m-%d %H:%s") as time,km_user_name as name,km_state as "state",km_update_time as uptime';
        // 分页
        $limit = $this->getPageing();
        // 判断是否是搜索
        if (isset($searchParams['content'])) {
            // 判断类型是否存在
            if (isset($searchParams['type'])) {
                if ($searchParams['type'] == 1) {
                    // 卡密模糊搜索
                    $where['kami_content'] = array('like', '%' . trim($searchParams['content']) . '%');
                } else {
                    // 使用者模糊搜索
                    $where['km_user_name'] = array('like', '%' . trim($searchParams['content']) . '%');
                }
            } else {
                // 模糊
                $where['kami_content'] = array('like', '%' . trim($searchParams['content']) . '%');
            }
            $data['data'] = db('v1_kami')->where($where)->field($sqlfield)->limit($limit)->order('kami_id desc')->select();
        } else {
            // 查询信息
            $data['data'] = db('v1_kami')->field($sqlfield)->limit($limit)->order('kami_id desc')->select();
        }
        // 获取表统计
        $data['count'] = db('v1_kami')->count();
        $data['code'] = 0;
        $data['msg'] = "搜索成功";
        return $data;
    }

    // 留言详情
    public function getStyInfo()
    {
        if (input('?post.id')) {
            $sqlfield = 'say_id as id,say_type as type,say_content as content,say_site as site,say_state as "state",DATE_FORMAT(say_time, "%Y-%m-%d %H:%s") as time';
            $data = db('v1_say')->where("say_to", input('post.id'))->field($sqlfield)->order('say_id desc')->select();
            return $data;
        }
    }

    // 留言列表
    public function getSayList()
    {
        //搜索
        $searchParams = json_decode(input('searchParams'), true);
        $sqlfield = 'say_id as id,say_type as type,say_content as content,say_site as site,say_state as "state",DATE_FORMAT(say_time, "%Y-%m-%d %H:%s") as time,v1_user.user_name as name';
        // 分页
        $limit = $this->getPageing();
        // 判断是否是搜索
        if (isset($searchParams['username'])) {
            // 模糊
            $where['say_content'] = array('like', '%' . trim($searchParams['username']) . '%');
            $data['data'] = db('v1_say')->where($where)->join('v1_user', 'v1_say.user_id = v1_user.user_id')->field($sqlfield)->limit($limit)->order('say_id desc')->select();
        } else {
            // 查询信息
            $data['data'] = db('v1_say')->join('v1_user', 'v1_say.user_id = v1_user.user_id')->field($sqlfield)->limit($limit)->order('say_id desc')->select();
        }
        // 获取表统计
        $data['count'] = db('v1_say')->count();
        $data['code'] = 0;
        return $data;
    }

    // 商城列表
    public function getMill()
    {
        try {
            // 验证
            $auth = config('requser_auth');
            $data = json_decode(file_get_contents(CMS_MILL_URL . "&auth=" . $auth), true);
            if (isset($data['code'])) {
                // 获取数据
                $install = getConfig('install')['install'];
                $install = explode(",", $install);
                
                // 数据初始化
                $install = $install == null ? [] : $install;
                $datas = $data;
                
                foreach ($data['data'] as $key => $value) {
                    $datas['data'][$key] = $value;
                    if (in_array($value['id'], $install)) {
                        $datas['data'][$key]['state'] = '';
                    } else {
                        $datas['data'][$key]['state'] = 1;
                    }
                }
                return $datas;
            } else {
                return ['code' => 0];
            }
        } catch (\Exception $th) {
            return ['code' => 0];
        }
    }

    // 广告列表
    public function getAdList()
    {
        // 设置别名
        $sqlfield = 'ad_id as id,ad_title as title,ad_content as content,ad_column as "column",ad_sort as "sort",ad_fixed as "fixed",ad_state as "state",DATE_FORMAT(ad_time, "%Y-%m-%d %H:%s") as time';
        // 分页
        $limit = $this->getPageing();
        $data['data'] = Db::table('v1_ad')->field($sqlfield)->limit($limit)->order('ad_id desc')->select();
        $data['code'] = 0;
        return $data;
    }

    // 侵权列表
    public function getTort()
    {
        // 设置别名
        $sqlfield = 'tort_id as id,tort_title as title,tort_site as url,tort_desc as "desc",tort_state as "state",DATE_FORMAT(tort_time, "%Y-%m-%d %H:%s") as time';
        // 分页
        $limit = $this->getPageing();
        // $data['data'] =  db('v1_tort')->field($sqlfield)->select();
        $data['data'] = Db::table('v1_tort')->field($sqlfield)->limit($limit)->order('tort_id desc')->select();
        $data['code'] = 0;
        return $data;
    }

    // 轮播列表
    public function getBannerList()
    {
        //搜索 
        $searchParams = json_decode(input('searchParams'), true);
        // 设置别名
        $sqlfield = 'banner_id as id,banner_title as title,banner_pic as pic,banner_url as url,banner_sort as sort,banner_type as type,banner_click as click,banner_close as close,DATE_FORMAT(banner_time, "%Y-%m-%d %H:%s") as time';
        // 转换查询信息
        // 分页
        $limit = $this->getPageing();
        // 判断是否是搜索
        if (isset($searchParams['username'])) {
            // 模糊
            $where['banner_title'] = array('like', '%' . $searchParams['username'] . '%');
            // 条件查询信息
            $data['data'] = Db::table('v1_banner')->where($where)->field($sqlfield)->limit($limit)->order('banner_id desc')->select();
        } else {
            // 查询信息
            $data['data'] = Db::table('v1_banner')->field($sqlfield)->limit($limit)->order('banner_id desc')->select();
            // 获取表统计
            $data['count'] = Db::table('v1_banner')->count();
        }
        // 成功响应码
        $data['code'] = 0;
        return $data;
    }

    // 专题列表
    public function getPlateList()
    {
        // 初始化参数
        $data = [];
        // 获取所有专题配置
        $filename = getFileName(APP_EXTRA_PATH, '.php');
        // 排序
        foreach ($filename as $key => $value) {
            $data['code'] = 0;
            $data['data'][$key] = config($value['val']);
        }
        return $data;
    }

    // 菜单列表
    public function getNav($Indexpage)
    {
        $view = new View();
        $path = APP_PATH . request()->module() . '/view/' . $Indexpage . "/" . $Indexpage . '.json';
        echo $view->fetch($path);
        return;
    }
}
